Completed
Pull Request — stable8.2 (#49)
by Olivier
09:04
created

$(document).ready   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
rs 9.4285

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 3 1
1
/* global OC, $, _, Gallery, SlideShow */
2
$(document).ready(function () {
3
	"use strict";
4
	$('#controls').insertBefore($('#content-wrapper'));
5
	Gallery.utility = new Gallery.Utility();
6
	Gallery.view = new Gallery.View();
7
	Gallery.token = Gallery.utility.getPublicToken();
8
	Gallery.ieVersion = Gallery.utility.getIeVersion();
9
10
	// The first thing to do is to detect if we're on IE
11
	if (Gallery.ieVersion === 'unsupportedIe') {
12
		Gallery.utility.showIeWarning(Gallery.ieVersion);
13
		Gallery.view.showEmptyFolder('', null);
14
	} else {
15
		if (Gallery.ieVersion === 'oldIe') {
16
			Gallery.utility.showIeWarning(Gallery.ieVersion);
17
		}
18
19
		// Get the config, the files and initialise the slideshow
20
		Gallery.view.showLoading();
21
		$.getJSON(Gallery.utility.buildGalleryUrl('config', '', {}))
22
			.then(function (config) {
23
				Gallery.config = new Gallery.Config(config);
24
				var currentLocation = window.location.href.split('#')[1] || '';
25
				Gallery.activeSlideShow = new SlideShow();
26
				$.when(
27
					Gallery.activeSlideShow.init(
28
						false,
29
						null,
30
						Gallery.config.galleryFeatures
31
					))
32
					.then(function () {
33
						Gallery.getFiles(currentLocation).then(function () {
34
							window.onhashchange();
35
						});
36
					});
37
			});
38
39
		$(document).click(function () {
40
			$('.album-info-container').slideUp();
41
		});
42
43
		// This block loads new rows
44
		$('html, #content-wrapper').scroll(function () {
45
			Gallery.view.loadVisibleRows(Gallery.albumMap[Gallery.currentAlbum]);
46
		});
47
48
49
		var windowWidth = $(window).width();
50
		var windowHeight = $(window).height();
51
		$(window).resize(_.throttle(function () {
52
			var infoContentContainer = $('.album-info-container');
53
			// This section redraws the photowall and limits the width of dropdowns
54
			if (windowWidth !== $(window).width()) {
55
				if ($('#emptycontent').is(':hidden')) {
56
					Gallery.view.viewAlbum(Gallery.currentAlbum);
57
					infoContentContainer.css('max-width', $(window).width());
58
				}
59
				if (Gallery.currentAlbum) {
60
					Gallery.view.breadcrumb.setMaxWidth($(window).width() - Gallery.buttonsWidth);
61
				}
62
63
				windowWidth = $(window).width();
64
			}
65
			// This makes sure dropdowns will not be hidden after a window resize
66
			if (windowHeight !== $(window).height()) {
67
				infoContentContainer.css('max-height',
68
					$(window).height() - Gallery.browserToolbarHeight);
69
70
				windowHeight = $(window).height();
71
			}
72
		}, 250)); // A shorter delay avoids redrawing the view in the middle of a previous request,
73
				  // but it may kill baby CPUs
74
	}
75
});
76
77
/**
78
 * Responsible to refresh the view when we detect a change of location via the browser URL
79
 */
80
window.onhashchange = function () {
81
	"use strict";
82
	Gallery.view.dimControls();
83
	var currentLocation = window.location.href.split('#')[1] || '';
84
	// The hash location is ALWAYS encoded, despite what the browser shows
85
	var path = decodeURIComponent(currentLocation);
86
87
	// This section tries to determine if the hash location points to a file or a folder
88
	var albumPath = OC.dirname(path);
89
	if (Gallery.albumMap[path]) {
90
		albumPath = path;
91
	} else if (!Gallery.albumMap[albumPath]) {
92
		albumPath = '';
93
	}
94
	// We need to get new files if we've assessed that we've changed folder
95
	if (Gallery.currentAlbum !== null && Gallery.currentAlbum !== albumPath) {
96
		Gallery.getFiles(currentLocation).done(function () {
97
			Gallery.refresh(path, albumPath);
98
		});
99
	} else {
100
		// When the gallery is first loaded, the files have already been fetched
101
		Gallery.refresh(path, albumPath);
102
	}
103
};
104